<script setup>
import { onMounted, reactive, ref, toRaw, watch } from 'vue'
import { searchProduct } from '@/service/product.js'
import { ElMessage } from 'element-plus'

const props = defineProps({
  type: {
    type: String,
    required: true,
    default: 'radio', // 可选值为 ['radio','section']
  },
  productType: {
    type: Number,
    required: false,
    default: 0,
    // 0-普通商品，1-秒杀商品
  },
})

const showProductSelectDialog = ref(false)
const productSearchForm = reactive({
  productName: '',
})

const productCurrentPage = ref(1)
const productPageSize = ref(10)
watch([productCurrentPage, productPageSize], async () => {
  const res = await searchProduct(
    productSearchForm.productName,
    'all',
    null,
    null,
    props.productType,
    productCurrentPage.value,
    productPageSize.value
  )
  productList.value = res.data.records
  productTotal.value = res.data.total
})

const productTotal = ref(0)
const productList = ref([])
async function onTapSearchProductButton() {
  const res = await searchProduct(
    productSearchForm.productName,
    'all',
    null,
    null,
    props.productType,
    productCurrentPage.value,
    productPageSize.value
  )
  productList.value = res.data.records
  productTotal.value = res.data.total
}
const productTableRef = ref()
async function onTapConfirmProduct() {
  const selectedProduct = productTableRef.value.getSelectionRows()
  if (!selectedProduct || selectedProduct.length === 0) {
    ElMessage.warning('请选择商品！')
    return
  }
  showProductSelectDialog.value = false
  emits('onTapConfirmProduct', selectedProduct)
}
const emits = defineEmits(['onTapConfirmProduct'])
defineExpose({
  showProductSelectDialog,
})

function handleSelectionChange(val) {
  if (props['type'] === 'section') {
    return
  }
  if (val.length > 1) {
    productTableRef.value.clearSelection()
    productTableRef.value.toggleRowSelection(val.pop())
  }
}
watch(showProductSelectDialog, async (newVal) => {
  if (!newVal) {
    productTableRef.value.clearSelection()
  } else {
    const res = await searchProduct(
      null,
      'all',
      null,
      null,
      props.productType,
      productCurrentPage.value,
      productPageSize.value
    )
    productList.value = res.data.records
    productTotal.value = res.data.total
  }
})
</script>

<template>
  <el-dialog
    v-model="showProductSelectDialog"
    :close-on-click-modal="false"
    append-to-body
    draggable
    title="选择商品"
    width="800"
  >
    <el-form :model="productSearchForm">
      <el-form-item label="商品名称:">
        <el-input v-model="productSearchForm.productName" placeholder="请输入商品名称" style="width: 240px"></el-input>
        <el-button style="margin-left: 10px" type="primary" @click="onTapSearchProductButton">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="productTableRef"
      :data="productList"
      max-height="450"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="商品信息" width="300">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <img :src="scope.row.image" alt="" style="width: 60px; height: 60px" />
            <div
              style="
                font-size: 12px;
                margin-left: 10px;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                overflow: hidden;
                text-overflow: ellipsis;
                line-height: 1.5; /* 可选：调整行高控制整体高度 */
                max-height: 3em; /* 2行 * 行高1.5 = 3em，可选但推荐配合使用 */
              "
            >
              {{ scope.row.productName }}
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="价格" prop="price">
        <template #default="scope">
          <span>￥{{scope.row.price}}</span>
        </template>
      </el-table-column>
      <el-table-column label="库存" prop="stock" />
      <el-table-column label="商品类型">
        <template #default="scope">
          <span v-if="scope.row.productType === 0">普通商品</span>
          <span v-if="scope.row.productType === 1">秒杀商品</span>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="productCurrentPage"
      v-model:page-size="productPageSize"
      :page-sizes="[10, 20, 30, 40]"
      :total="productTotal"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top: 20px"
    />
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="onTapConfirmProduct">确认</el-button>
        <el-button @click="showProductSelectDialog = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped></style>
